package com.zs.grain.ui

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.xnkj1688.semplibrary.entity.Menu
import com.xnkj1688.semplibrary.mvvm.BaseViewModel
import java.text.DecimalFormat
import javax.inject.Inject


/**
 * Created by Wesley on 2017/7/14.
 */
class MenuViewModel @Inject constructor(
    val mApp: Application,
    val mMenu: Menu
) : BaseViewModel(mApp) {
    val name = MutableLiveData<String>()
    val name2 = MutableLiveData<String>()
    val price = MutableLiveData<String>()//Float
    val oldPrice = MutableLiveData<String>()
    val enable = MutableLiveData<Boolean>()
    val beforeEnable = MutableLiveData<Boolean>()
    val uri = MutableLiveData<String>()
    val remark = MutableLiveData<String>()
    init {
        name.value = mMenu.name
        name2.value = mMenu.name2
//        if(((mMenu.price%10)==0)) {
//            //最后一位为0
//            if((mMenu.price/10%10)==0) {
//                price.value = (mMenu.price / 100).toString()
//                //oldPrice.value = (mMenu.price / 100 + 2).toString()
//            }//不保留小数
//            else {
//                val myformat = DecimalFormat("0.0")
//                price.value = myformat.format(mMenu.price.toDouble() / 100.0).toString()
//                //oldPrice.value = myformat.format(mMenu.price.toDouble() / 100.0 + 2.0).toString()
//            }//.toString()//保留1位小数
//        }else {
//            price.value = (mMenu.price.toDouble() / 100.0).toString()//最后一位不为0,保留2位小数
//            //oldPrice.value = (mMenu.price.toDouble() / 100.0 + 2.0).toString()
//        }
        remark.value = mMenu.remark
        price.value = withNoZeroEnd(mMenu.price)
        oldPrice.value = withNoZeroEnd(mMenu.price2)
        enable.value = false
        beforeEnable.value = false
    }

    fun withNoZeroEnd(price:Int):String{
        var resultPriceString = ""
        if(((price%10)==0)) {
            //最后一位为0
            if((price/10%10)==0) {
                resultPriceString = (price / 100).toString()
            }//不保留小数
            else {
                val myformat = DecimalFormat("0.0")
                resultPriceString = myformat.format(price.toDouble() / 100.0).toString()
            }//.toString()//保留1位小数
        }else {
            resultPriceString = (price.toDouble() / 100.0).toString()//最后一位不为0,保留2位小数
        }
        return resultPriceString
    }
}
